package pw.yumc.Residence.commandsub;

import org.bukkit.ChatColor;
import org.bukkit.Location;
import org.bukkit.command.Command;
import org.bukkit.command.CommandException;
import org.bukkit.command.CommandSender;
import org.bukkit.entity.Player;

import com.bekvon.bukkit.residence.protection.ResidenceManager;

import cn.citycraft.PluginHelper.commands.BaseCommand;
import pw.yumc.Residence.ResidenceMain;
import pw.yumc.Residence.text.Language;

public class CommandCompass extends BaseCommand {
    ResidenceMain plugin;

    public CommandCompass(final ResidenceMain plugin) {
        super("cp");
        this.plugin = plugin;
        setOnlyPlayerExecutable();
        setDescription("切换指南针方向");
        setPossibleArguments("[领地名称]");
    }

    @Override
    public void execute(final CommandSender sender, final Command command, final String label, final String[] args) throws CommandException {
        final Player player = (Player) sender;
        final ResidenceManager rmanager = plugin.getResidenceManager();
        final Language language = plugin.getLanguage();

        if (args.length != 1) {
            player.setCompassTarget(player.getWorld().getSpawnLocation());
            player.sendMessage(ChatColor.GREEN + language.getPhrase("CompassTargetReset"));
            return;
        }
        final String resname = args[0];

        if (rmanager.getByName(resname) != null) {
            if (rmanager.getByName(resname).getWorld().equalsIgnoreCase(player.getWorld().getName())) {
                final Location low = rmanager.getByName(resname).getArea("main").getLowLoc();
                final Location high = rmanager.getByName(resname).getArea("main").getHighLoc();
                final Location mid = new Location(low.getWorld(), (low.getBlockX() + high.getBlockX()) / 2, (low.getBlockY() + high.getBlockY()) / 2, (low.getBlockZ() + high.getBlockZ()) / 2);
                player.setCompassTarget(mid);
                player.sendMessage(ChatColor.GREEN + language.getPhrase("CompassTargetSet", ChatColor.YELLOW + resname + ChatColor.GREEN));
            }
        } else {
            player.sendMessage(ChatColor.RED + language.getPhrase("InvalidResidence"));
        }
    }

}
